package org.bingo.controller;

import org.bingo.misc.Result;
import org.bingo.misc.ResultState;
import org.bingo.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private FileService fileService;

    @PostMapping("/avatar")
    public Result avatar(@RequestParam("avatar") MultipartFile avatar) {
        String avatarUrl = fileService.uploadAvatar(avatar);
        if (avatarUrl == null) {
            return Result.build(ResultState.UPLOAD_AVATAR_FAIL, null);
        }
        return Result.build(ResultState.OK, avatarUrl);
    }

    @PostMapping("/cover")
    public Result cover(@RequestParam("cover") MultipartFile cover) {
        String coverUrl = fileService.uploadCover(cover);
        if (coverUrl == null) {
            return Result.build(ResultState.UPLOAD_COVER_FAIL, null);
        }
        return Result.build(ResultState.OK, coverUrl);
    }

    @PostMapping("/video")
    public Result video(@RequestParam("video") MultipartFile video) {
        String m3u8Url = fileService.uploadVideo(video);
        if (m3u8Url == null) {
            return Result.build(ResultState.UPLOAD_VIDEO_FAIL, null);
        }
        return Result.build(ResultState.OK, m3u8Url);
    }
}
